package grsu.epam.airline.service;

import grsu.epam.airline.DAO.IFlightCrewDAO;
import grsu.epam.airline.DAOImpl.DAOFactory;
import grsu.epam.airline.constant.Constant;
import grsu.epam.airline.model.FlightCrew;
import java.sql.SQLException;
import java.util.ArrayList;

public class FlightCrewService {
	public void addFlightCrew(String flightCrewName) {
		IFlightCrewDAO flightCrewDao = (IFlightCrewDAO) DAOFactory
				.getInstance().getDAO(Constant.FlightCrewDAO);
		FlightCrew flightCrew = new FlightCrew(flightCrewName);
		try {
			flightCrewDao.addFlightCrew(flightCrew);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					flightCrewDao.getConnection());
		}
	}

	public ArrayList<FlightCrew> getAllFlightCrew() {
		IFlightCrewDAO flightCrewDao = (IFlightCrewDAO) DAOFactory
				.getInstance().getDAO(Constant.FlightCrewDAO);
		ArrayList<FlightCrew> crews = null;

		try {
			crews = (ArrayList<FlightCrew>) flightCrewDao.getAllFlightCrew();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					flightCrewDao.getConnection());
		}
		return crews;
	}

	public void deleteFlightCrew(int flightCrewId) {
		IFlightCrewDAO flightCrewDao = (IFlightCrewDAO) DAOFactory
				.getInstance().getDAO(Constant.FlightCrewDAO);
		try {
			flightCrewDao.deleteFlightCrew(flightCrewId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					flightCrewDao.getConnection());
		}
	}

	public FlightCrew getFlightCrewById(int flightCrewId) {
		IFlightCrewDAO flightCrewDao = (IFlightCrewDAO) DAOFactory
				.getInstance().getDAO(Constant.FlightCrewDAO);
		FlightCrew flightCrew = null;

		try {
			flightCrew = (FlightCrew) flightCrewDao
					.getFlightCrewById(flightCrewId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					flightCrewDao.getConnection());
		}
		return flightCrew;
	}

	public void updateFlightCrew(int flightCrewId, int employeeId) {
		IFlightCrewDAO flightCrewDao = (IFlightCrewDAO) DAOFactory
				.getInstance().getDAO(Constant.FlightCrewDAO);
		try {
			flightCrewDao.updateFlightCrew(flightCrewId, employeeId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					flightCrewDao.getConnection());
		}
	}
}
